local xiaoxiang = fk.CreateSkill {
  name = "kl__xiaoxiang",
}

Fk:loadTranslationTable {
  ["kl__xiaoxiang"] = "枭降",
  [":kl__xiaoxiang"] = "当你连续弃置一名其他角色共计两张牌后（不可嵌套），你可以对其造成1点伤害。当你受到或造成伤害时，你可以重铸一张牌并摸X张牌（X为你的攻击范围且至多为2）。",

  ["#kl__xiaoxiang-dam"] = "枭降：是否对 %dest 造成1点伤害？",
  ["#kl__xiaoxiang-draw"] = "枭降：你可以重铸一张牌然后摸%arg张牌",

  ["$kl__xiaoxiang1"] = "莫欺我年幼，勿谓言之不预也。",

}

xiaoxiang:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(xiaoxiang.name) and player:getMark(xiaoxiang.name) >= 2 then
      for _, move in ipairs(data) do
        if move.from and move.from ~= player and move.proposer == player and
        move.moveReason == fk.ReasonDiscard and player:getMark("kl__xiaoxiang_to") == move.from.id then
          event:setCostData(self, { to = move.from })
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local to = event:getCostData(self).to
    return player.room:askToSkillInvoke(player, { skill_name = xiaoxiang.name, prompt = "#kl__xiaoxiang-dam::" .. to.id })
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, xiaoxiang.name, 0)
    player.room:damage {
      from = player,
      to = event:getCostData(self).to,
      damage = 1,
      skillName = xiaoxiang.name,
    }
  end,

  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(xiaoxiang.name) then
      local ids = {}
      for _, move in ipairs(data) do
        if move.from and move.from ~= player and move.proposer == player and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      if #ids == 1 or #ids == 2 then
        return true
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local ids = {}
    for _, move in ipairs(data) do
      if move.from and move.from ~= player and move.proposer == player and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            table.insertIfNeed(ids, info.cardId)
          end
        end
        local now = player:getMark("kl__xiaoxiang_to")
        if now == 0 then
          now = move.from.id
        end
        if now ~= move.from.id then
          player.room:setPlayerMark(player, xiaoxiang.name, 0)
        end
        if #ids == 1 then
          player.room:addPlayerMark(player, xiaoxiang.name, 1)
        elseif #ids == 2 then
          player.room:addPlayerMark(player, xiaoxiang.name, 2)
        end
        player.room:setPlayerMark(player, "kl__xiaoxiang_to", move.from.id)
      end
    end
  end,
})


local spec = {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(xiaoxiang.name) and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local num = math.min(player:getAttackRange(), 2)
    local card = player.room:askToCards(player, {
      max_num = 1,
      min_num = 1,
      skill_name = xiaoxiang.name,
      cancelable = true,
      include_equip = true,
      prompt = "#kl__xiaoxiang-draw:::" .. num,
    })
    if #card > 0 then
      event:setCostData(self, { cards = card, num = num })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recastCard(event:getCostData(self).cards, player, xiaoxiang.name)
    room:drawCards(player, event:getCostData(self).num, xiaoxiang.name)
  end,
}

xiaoxiang:addEffect(fk.DamageCaused, spec)
xiaoxiang:addEffect(fk.DamageInflicted, spec)


return xiaoxiang
